const express = require('express');
const app = express()
const md5 = require('md5')
var request = require('request');
var rp = require('request-promise');
var log4js = require('log4js')

const schedule = require('node-schedule');

const audit = require('./methods/audit.js')
const remind = require('./methods/remind')


Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


var timestamp = new Date().getTime()
var sign = md5('admin' + timestamp + '5fec4ba8376f207d1ff2f0cac0882b01')

global.sign = sign
global.timestamp = timestamp

var ruleA = new schedule.RecurrenceRule();
ruleA.second = 0
ruleA.minute = [0,5,10,15,20,25, 30,35,40,45,50,55]
schedule.scheduleJob(ruleA, () => {
    audit.setIntervalFun() //调用审核
});


var a1 = null
var a2 = null
var ruleS = new schedule.RecurrenceRule();
ruleS.second = 0
ruleS.minute = [0, 30]
schedule.scheduleJob(ruleS, () => {
    // console.log(1)
    remind.getStatistical((isTrue,str)=>{
        if (isTrue) {
            set1()
        } else {
            set2()
        }
    })
});


function set1() {
    if (a1 == null || a1.pendingInvocations.length == 0) {
        if (a2) {
            a2.cancel()
        }
        var rule = new schedule.RecurrenceRule();
        rule.second = 30
        rule.minute = 0
        rule.hour = [9,10,11,12,13,14,15,16,17,18,19,20,21,22]
        a1 = schedule.scheduleJob(rule, () => {
            remind.sendText()
            // console.log(1)
        });
    }
}


function set2() {
    if (a2 ==null || a2.pendingInvocations.length == 0) {
        if (a1) {
            a1.cancel()
        }
        var rule = new schedule.RecurrenceRule();
        rule.second = 30
        rule.minute = 30
        rule.hour = [8,10,12,14,16,18,20,22]
        a2 = schedule.scheduleJob(rule, () => {
            remind.sendText()
            // console.log(2)
        });    
    }
}



app.listen(3001, () => {
    console.log('服务启动成功')
})

